<%@ page import="java.net.URLEncoder" %>
<%@ page import="com.aiden.utils.CookieUtil" %>
<%--
  服务端：处理客户端请求
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
 <%
     //1.处理乱码
     request.setCharacterEncoding("UTF-8");
     //2.获取用户名密码
     String username = request.getParameter("username");
     String password=request.getParameter("password");
     //3.逻辑验证用户名密码是否正确 t161 123456
     if("t161".equalsIgnoreCase(username)&&"123456".equals(password)){
         //将用户名存储在session
         session.setAttribute("sessionUserName",username);
         session.setAttribute("sessionFullName","杨华雨");
         session.setMaxInactiveInterval(1*60);//设置有效期为10秒

         //处理cookie的存储
         String autoLogin = request.getParameter("autoLogin");
         if(autoLogin!=null){
             //存储cookie
             CookieUtil.setCookieVal("username",username,2*60,response);
             CookieUtil.setCookieVal("password",password,2*60,response);
         }else{
             //设置cookie失效
             CookieUtil.removeCookie("username",request);
             CookieUtil.removeCookie("password",request);

         }


         response.sendRedirect("welcome.jsp");

         //3.1跳转首页面
         /*重定向--->实现页面跳转*/
         //response.sendRedirect("https://www.baidu.com/");/*重定向*/
         //response.sendRedirect("welcome.jsp?loginName="+username+"&fullName="+ URLEncoder.encode("帝释天","UTF-8"));/*重定向*/

         //转发：
         //request.getRequestDispatcher("https://www.baidu.com/").forward(request,response);
     }else{
         //out.print("<script>alert('用户名或密码错误！');location='login.jsp';</script>");
         //存储要提示的登录错误信息
         request.setAttribute("errorMsg","用户名或密码错误！");
         /*转发--->实现页面跳转*/
         request.getRequestDispatcher("login.jsp").forward(request,response);
         //response.sendRedirect("login.jsp");/*重定向*/

     }
 %>
